//
//  EJSHelpCenterViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/13.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSHelpCenterViewController.h"
#import "EJSHelpCenterDetailViewController.h"
#import "EJSArticleListRequest.h"

@interface EJSHelpCenterTableViewCell:UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@end

@implementation EJSHelpCenterTableViewCell

@end


@interface EJSHelpCenterViewController ()
{
    NSInteger _currentPage;
}

@end

@implementation EJSHelpCenterViewController

+ (instancetype)viewController
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    return [storyBoard instantiateViewControllerWithIdentifier:@"EJSHelpCenterViewController"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getArticleList];
    self.title = @"帮助中心";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configOwnViews{
    
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.estimatedRowHeight = 50;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 0;
        [self getArticleList];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage ++;
        [self getArticleList];
    }];
}

- (void)getArticleList{
    EJSArticleListRequest *req = [EJSArticleListRequest new];
    req.page = @(_currentPage).stringValue;
    req.pageLimit = @"20";
    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        EJSArticleListModel *listModel = response.data;
        if(listModel){
            if(listModel.totalPages.integerValue == listModel.page.integerValue){
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            if(_currentPage == 0){
                weakSelf.dataSource = [NSMutableArray arrayWithArray:listModel.list];
            }else{
                [weakSelf.dataSource addObjectsFromArray:listModel.list];
            }
        }
        
        if(weakSelf.dataSource.count > 0){
            [weakSelf hideNoDataView];
        }else{
            [weakSelf showNoDataViewWithText:@"暂无帮助文档"];
        }
        [weakSelf.tableView reloadData];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EJSHelpCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSHelpCenterTableViewCell" forIndexPath:indexPath];
    EJSArticleModel *model = self.dataSource[indexPath.row];
    cell.titleLabel.text = model.title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    EJSArticleModel *model = self.dataSource[indexPath.row];
    EJSHelpCenterDetailViewController *detailVC = [[EJSHelpCenterDetailViewController alloc] initWithNibName:@"EJSHelpCenterDetailViewController" bundle:nil];
    detailVC.title = model.title;
    detailVC.articleId = model.articleId;
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
